package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.handler.ListTypeHandler;
import com.fudan.backend.pojo.StoreDeleteRequest;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StoreDeleteRequestDao {
    @Results(id = "storeDeleteRequestMap", value = {
            @Result(column = "store_delete_request_id", property = "storeDeleteRequestId", typeHandler = IdTypeHandler.class),
            @Result(column = "initiate_time", property = "initiateTime"),
            @Result(column = "process_time", property = "processTime"),
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "status", property = "status"),
            @Result(column = "info", property = "info"),
    })
    @Select("select * from t_store_delete_request")
    List<StoreDeleteRequest> selectAllStoreDeleteRequest();

    @ResultMap("storeDeleteRequestMap")
    @Select("select * from t_store_delete_request where store_delete_request_id = #{storeDeleteRequestId}")
    StoreDeleteRequest selectStoreDeleteRequestByStoreDeleteRequestId(String storeDeleteRequestId);

    @ResultMap("storeDeleteRequestMap")
    @Select("select * from t_store_delete_request where store_id = #{storeId}")
    List<StoreDeleteRequest>  selectAllStoreDeleteRequestByStoreId(String storeId);

    @ResultMap("storeDeleteRequestMap")
    @Select("<script>" +
            "select * from t_store_delete_request " +
            "where store_id in " +
            "<foreach collection='storeIdList' item='id' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "</foreach>" +
            "</script>")
    List<StoreDeleteRequest> selectStoreDeleteRequestByStoreIdList(List<String> storeIdList);

    @Select("select COUNT(*) from t_store_delete_request where " +
            "store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler} and status != 2")
    int selectStoreDeleteRequestCountByStoreName(String storeId);

    @Insert("insert into t_store_delete_request values " +
            "(#{storeDeleteRequestId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{initiateTime}, #{processTime}, " +
            "#{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{status}, #{info})")
    int insertStoreDeleteRequest(StoreDeleteRequest storeDeleteRequest);

    @Update("update t_store_delete_request set " +
            "initiate_time = #{initiateTime}, process_time = #{processTime}, " +
            "store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "status = #{status}, info = #{info} " +
            "where store_delete_request_id = #{storeDeleteRequestId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int updateStoreDeleteRequest(StoreDeleteRequest storeDeleteRequest);
}
